package com.chenyaowei.web.controller.product.category;

import com.chenyaowei.common.annotation.Anonymous;
import com.chenyaowei.common.annotation.Log;
import com.chenyaowei.common.constant.HttpStatus;
import com.chenyaowei.common.core.controller.BaseController;
import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.common.core.page.TableDataInfo;
import com.chenyaowei.common.enums.BusinessType;
import com.chenyaowei.common.utils.poi.ExcelUtil;
import com.chenyaowei.product.domain.CywProductCategory;
import com.chenyaowei.product.service.ICywProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品分类Controller
 *
 * @author 陈耀威
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/product/category")
public class CywProductCategoryController extends BaseController {
    @Autowired
    private ICywProductCategoryService cywProductCategoryService;

    private final Long MAX_LEVEL = 3L;

    @Anonymous
    @GetMapping("/origin/{catId}")
    public AjaxResult findOriginCategories(@PathVariable("catId") Long catId) {
        return success(cywProductCategoryService.findOriginCategories(catId));
    }


    /**
     * 查询树形商品分类
     */
//    @PreAuthorize("@ss.hasPermi('product:category:list')")
    @GetMapping("/treeCategories")
    public AjaxResult treeCategories() {
        return success(cywProductCategoryService.selectTreeCategoryList(new CywProductCategory()));
    }

    /**
     * 查询商品分类列表
     */
//    @PreAuthorize("@ss.hasPermi('product:category:query')")
    @GetMapping("/level1")
    public AjaxResult getLevel1Categories() {
        CywProductCategory cywProductCategory = new CywProductCategory();
        cywProductCategory.setCatLevel(1L);
        List<CywProductCategory> list = cywProductCategoryService.selectTreeCategoryList(cywProductCategory);
        return success(list);
    }

    /**
     * 查询树形商品分类
     */
//    @PreAuthorize("@ss.hasPermi('product:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(CywProductCategory cywProductCategory, Integer pageNum, Integer pageSize) {
//        startPage();
        List<CywProductCategory> list = cywProductCategoryService.selectTreeCategoryList(cywProductCategory);
        Integer total = list.size();
        Integer start = (pageNum - 1) * pageSize;
        Integer end = pageNum * pageSize - 1;
        if (end > total){
            // [10,19)
            list = list.subList(start, total);
        }else {
            list = list.subList(start, end + 1);
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
//        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('product:category:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CywProductCategory cywProductCategory) {
        List<CywProductCategory> list = cywProductCategoryService.selectCywProductCategoryList(cywProductCategory);
        ExcelUtil<CywProductCategory> util = new ExcelUtil<CywProductCategory>(CywProductCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:category:query')")
    @GetMapping(value = "/{catId}")
    public AjaxResult getInfo(@PathVariable("catId") Long catId) {
        return success(cywProductCategoryService.selectCywProductCategoryByCatId(catId));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('product:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CywProductCategory cywProductCategory) {
        return toAjax(cywProductCategoryService.insertCywProductCategory(cywProductCategory));
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('product:category:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CywProductCategory cywProductCategory) {
        Long parentCid = cywProductCategory.getParentCid();
        if (parentCid == 0L){
            cywProductCategory.setCatLevel(1L);
        }else {
            CywProductCategory parentCategory = cywProductCategoryService.selectCywProductCategoryByCatId(parentCid);
            if (parentCategory.getCatLevel() + 1L > MAX_LEVEL){
                return AjaxResult.error("商品分类层级不能超过3层");
            }
            cywProductCategory.setCatLevel(parentCategory.getCatLevel() + 1);

        }
        return toAjax(cywProductCategoryService.updateCywProductCategory(cywProductCategory));
    }

    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('product:category:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{catIds}")
    public AjaxResult remove(@PathVariable Long[] catIds) {
        return toAjax(cywProductCategoryService.deleteCywProductCategoryByCatIds(catIds));
    }
}
